package fortunata.jspwikiplugins.mig;
import java.util.*;


import com.ecyrd.jspwiki.*;
import com.ecyrd.jspwiki.plugin.*;
import net.sourceforge.wurfl.wurflapi.*;

import org.apache.log4j.Logger;
import fortunata.FortunataPlugin;
import javax.servlet.ServletContext;


/**
 * Creates wiki pages for the added ontology. Also generates the semantic data, the results-destination wiki page, and
 * the checks wiki page.
 */
public class ShowDeviceFromUA extends FortunataPlugin implements WikiPlugin {

    private static Logger log = Logger.getLogger(ShowDeviceFromUA.class );

    public String execute (WikiContext context, Map parameterMap) throws PluginException {
        ServletContext sc = context.getEngine().getServletContext();
        ObjectsManager.initFromWebApplication(sc); // This sentence loads the wurfl.xml file from WEB-INF.
                                                   // It is big... use -Xms128m -Xmx128m
                                                   // This should be in the Init Servlet....
        String wikiTextMessage;
        StringBuffer sb = new StringBuffer();

        UAManager uam = ObjectsManager.getUAManagerInstance();
        CapabilityMatrix cm = ObjectsManager.getCapabilityMatrixInstance();
        String uaStr = context.getHttpRequest().getHeader("User-Agent");
        String deviceStr = uam.getDeviceIDFromUALoose(uaStr); //Looks for the closer device

        sb.append("User Agent (UA): " + uaStr + wnl);
        sb.append("Device using UA:" + uam.getDeviceIDFromUA(uaStr) + wnl);
        sb.append("Device using UALoose:" + uam.getDeviceIDFromUALoose(uaStr) + wnl);
        sb.append("Brand and model:" + cm.getCapabilityForDevice(deviceStr,"brand_name")  + ", " 
                                     + cm.getCapabilityForDevice(deviceStr,"model_name")  + wnl);
        sb.append("width ="             + cm.getCapabilityForDevice(deviceStr,"resolution_width")  + wnl);
        sb.append("height ="            + cm.getCapabilityForDevice(deviceStr,"resolution_height") + wnl);
        sb.append("columns ="           + cm.getCapabilityForDevice(deviceStr,"columns")           + wnl);
        sb.append("max_image_width ="   + cm.getCapabilityForDevice(deviceStr,"max_image_width")   + wnl);
        sb.append("max_image_height ="  + cm.getCapabilityForDevice(deviceStr,"max_image_height")  + wnl);
        //Show results message
        wikiTextMessage = sb.toString();
        return renderWikiText(context, wikiTextMessage);
    }
}